﻿using Newtonsoft.Json;
using System.IO;

namespace SyncUserAccount
{
    public class Do
    {
        public static void SendMails()
        {
            //var configPath = System.Configuration.ConfigurationSettings.AppSettings["QuickPath"];
            var configPath = System.Configuration.ConfigurationSettings.AppSettings["ConfigPath"];

            var files = Directory.GetFiles(configPath, "*.json");

            foreach (var file in files)
            {
                using (FileStream fs = new FileStream(file, FileMode.Open))
                {
                    StreamReader sr = new StreamReader(fs);
                    var strJson = sr.ReadToEnd();
                    sr.Close();

                    var json = JsonConvert.DeserializeObject<Config>(strJson);
                    SendMail(json);
                }
            }
        }

        public static void SendMailsWithBody(string Body)
        {
            //var configPath = System.Configuration.ConfigurationSettings.AppSettings["QuickPath"];
            var configPath = System.Configuration.ConfigurationSettings.AppSettings["ConfigPath"];

            var files = Directory.GetFiles(configPath, "*.json");

            foreach (var file in files)
            {
                using (FileStream fs = new FileStream(file, FileMode.Open))
                {
                    StreamReader sr = new StreamReader(fs);
                    var strJson = sr.ReadToEnd();
                    sr.Close();

                    var json = JsonConvert.DeserializeObject<Config>(strJson);
                    json.Email.Body = Body;
                    SendMail(json);
                }
            }
        }

        public static void SendMails(string ConfigPath)
        {
            var configPath = System.Configuration.ConfigurationSettings.AppSettings[ConfigPath];
            var files = Directory.GetFiles(configPath, "*.json");

            foreach (var file in files)
            {
                using (FileStream fs = new FileStream(file, FileMode.Open))
                {
                    StreamReader sr = new StreamReader(fs);
                    var strJson = sr.ReadToEnd();
                    sr.Close();

                    var json = JsonConvert.DeserializeObject<Config>(strJson);
                    SendMail(json);
                }
            }
        }

        public static void SendMail(Config config)
        {
            EmailHelper.SendMail(config.Email);
        }
    }

}
